package com.example.tangjiangxu.servicetest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private LinearLayout activityMain;
    private Button bnStart;
    private Button bnStop;
    private Button bnBind;
    private Button bnUnbind;

    /**
     * 自定义的Serviceconnection
     */
    ServiceConnection serviceConnection = new ServiceConnection() {
        /**
         * Activity与Service建立关联的时候调用
         * @param componentName
         * @param iBinder
         */
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            MyService.MyBinder myBinder = (MyService.MyBinder) iBinder;
            myBinder.startDownload();
        }

        /**
         * Activity与Service解除关联的时候调用
         * @param componentName
         */
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: Activity thread id is: " + Thread.currentThread().getId());
        setContentView(R.layout.activity_main);
        activityMain = (LinearLayout) findViewById(R.id.activity_main);
        bnStart = (Button) findViewById(R.id.bn_start);
        bnStop = (Button) findViewById(R.id.bn_stop);
        bnBind = (Button) findViewById(R.id.bn_bind);
        bnUnbind = (Button) findViewById(R.id.bn_unbind);
        bnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService();
            }
        });
        bnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService();
            }
        });
        bnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bindService();
            }
        });
        bnUnbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unBindService();
            }
        });
    }

    /**
     * startService的方式启动Service
     */
    private void startService() {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    /**
     * stopService的方式停止Service
     */
    private void stopService() {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }

    /**
     * bindServer方式启动Service
     */
    private void bindService() {
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }

    /**
     * unBindServer方式解绑Service
     * 只有第一次调用unbindService有用，多次调用会报错
     */
    private void unBindService() {
        unbindService(serviceConnection);
    }

}
